#include "head.h"

int main(int argc, char *argv[]){

    /*char ip[100];
    bzero(ip, sizeof(ip));
    getparameter("ip", ip);

    char port[100];
    bzero(port, sizeof(port));
    getparameter("port", port);*/

    ARGS_CHECK(argc, 3);
    char *ip = argv[1];
    char *port = argv[2];

    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(socket_fd, -1, "socket");
    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(atoi(port));
    sockaddr.sin_addr.s_addr = inet_addr(ip);

    int res_conn = connect(socket_fd,(struct sockaddr *)&sockaddr,sizeof(sockaddr));
    ERROR_CHECK(res_conn, -1, "connect");

    // 登陆到服务器
    login(socket_fd);

    fd_set set;
    char stdin_buf[1024] = {0}; // 标准输入
    char back_buf[100] = {0}; // 备份
    while(1){
        FD_ZERO(&set);
        FD_SET(STDIN_FILENO, &set);
        //FD_SET(socket_fd, &set);
        select(10, &set, NULL, NULL, NULL);
        //客户输入指令
        if(FD_ISSET(STDIN_FILENO,&set)){
            bzero(stdin_buf,sizeof(stdin_buf));
            int ret = read(STDIN_FILENO, stdin_buf, sizeof(stdin_buf));
            memcpy(back_buf, stdin_buf, sizeof(back_buf));
            if(ret == 0){
                break;
            }
            //分析指令
            //TODO
            //ch存储指令
            char *ch = strtok(stdin_buf, " ");
            char ch1[100]={0};
            strncpy(ch1, ch, strlen(ch) - 1);

            //printf("指令是：%s\n",ch);
            //p存储路径
            char *p = strtok(NULL, "/");

            if((strcmp(ch,"cd")==0)||(strcmp(ch,"rm")==0)
               ||(strcmp(ch,"mkdir")==0)){
                char res_buf[1024] = {0};
                send(socket_fd, back_buf, sizeof(back_buf), 0);
                recv(socket_fd, res_buf, sizeof(res_buf), 0);
                printf("%s\n", res_buf);
            }
            else if(strcmp(ch1,"ls")==0&&p==NULL){
                send(socket_fd,back_buf,sizeof(back_buf),0);
                char res_buf[1024] = {0};
                while(1){

                    recv(socket_fd, res_buf, sizeof(res_buf), 0);
                    if(strncmp(res_buf, "1", 1) == 0){
                        break;
                    }
                    printf("%s\n", res_buf);
                }
                //printf("while over!\n");
            }
            else if(strcmp(ch1,"ll")==0&&p==NULL){
                send(socket_fd,back_buf,sizeof(back_buf),0);
                char res_buf[1024] = {0};
                recv(socket_fd, res_buf, sizeof(res_buf), 0);
                printf("%s\n", res_buf);
            }
            else if(strcmp(ch1,"pwd")==0&&p==NULL){
                send(socket_fd,back_buf,sizeof(back_buf),0);
                char res_buf[1024] = {0};
                recv(socket_fd, res_buf, sizeof(res_buf), 0);
                printf("%s\n", res_buf);
            }
            // 从客户端client 上传到 用户smile
            else if(strcmp(ch, "upload") == 0){
                //文件名
                char filename[100]={0};
                strncpy(filename, p, strlen(p)-1);
                printf("file name : %s\n", filename);
                int fd = open(filename, O_RDONLY);
                if(fd == -1){
                    printf("文件不存在\n");
                    continue;
                }
                send(socket_fd, back_buf, sizeof(back_buf), 0); 
                send_bigfile(socket_fd, filename);
            } 
            // 从用户smile 下载到 客户端client
            else if(strcmp(ch, "download") == 0){ 
                printf("download start\n");
                send(socket_fd, back_buf, sizeof(back_buf), 0); // 将命令发给服务器
                //printf("%s\n", back_buf);
                char filename[100]={0};
                //printf("%s\n", p);
                strncpy(filename, p, strlen(p)-1);
                //printf("%s\n", filename);
                recv_bigfile(socket_fd, filename);
                printf("download over\n");
            } else if((strcmp(ch1, "undorm") == 0)&&p==NULL){
                send(socket_fd,back_buf,sizeof(back_buf),0);
                char res_buf[1024] = {0};
                recv(socket_fd, res_buf, sizeof(res_buf), 0); 
                printf("%s\n", res_buf);
            }   
        }
    }

    close(socket_fd);

    return 0;
}
